package com.example.swd1.gudong.view;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-29
//   Role :    None
//   Reference documents : None
//==================================================================================================================

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.example.swd1.gudong.R;

public class StartView extends View{


    private Paint mPaint;
    private String text = "开始跑步";
    private float leftOffset = 0;
    private float radius = 0;
    private Bitmap bitmapStrat;
    private boolean isClickable = true;
    private onButtonClickListener mOnButtonClickListener;
    WindowManager windowManager;

    public StartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Log.d("555log",windowManager.getDefaultDisplay().getWidth()+","+windowManager.getDefaultDisplay().getHeight());
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bitmapStrat = BitmapFactory.decodeResource(getResources(), R.drawable.ic_sport_start);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(getResources().getColor(R.color.codoon_no_alpha_green));
        Path path = new Path();
        path.lineTo(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        path.lineTo(getMeasuredWidth(), 0);
        path.lineTo(0, 0);
        path.close();
        canvas.drawPath(path, mPaint);


        leftOffset = getMeasuredWidth() / 2;
        radius = bitmapStrat.getWidth() / 2;
        canvas.drawBitmap(bitmapStrat, getMeasuredWidth() / 2 - bitmapStrat.getWidth() / 2, 0, mPaint);

        TextPaint textPaint = new TextPaint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(bitmapStrat.getWidth() / 6);
        StaticLayout layout = new StaticLayout(text, textPaint, bitmapStrat.getWidth() / 3, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, true);
        canvas.translate(getMeasuredWidth() / 2 - textPaint.measureText(text) / 4, bitmapStrat.getHeight() / 2 - textPaint.measureText(text)/3);
        layout.draw(canvas);


        canvas.translate(-(getMeasuredWidth() / 2 - textPaint.measureText(text) / 4), -(bitmapStrat.getHeight() / 2 - textPaint.measureText(text)/3));

        Bitmap bitmapGps = BitmapFactory.decodeResource(getResources(), R.drawable.ic_sport_gps_connect_weak);
        canvas.drawBitmap(bitmapGps, 0, getMeasuredHeight() - bitmapGps.getHeight() - 5, mPaint);


        String aqi = "空气 良";
        Bitmap bitmapAqi = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pm25);
        mPaint.setTextSize(windowManager.getDefaultDisplay().getWidth()/25);
        mPaint.setColor(Color.GRAY);
        canvas.drawText(aqi, getMeasuredWidth() - mPaint.measureText(aqi) - 30, getMeasuredHeight() - 20, mPaint);
        canvas.drawBitmap(bitmapAqi, getMeasuredWidth() - bitmapAqi.getWidth() - mPaint.measureText(aqi) - 50,
                getMeasuredHeight() - bitmapAqi.getHeight() - 20, mPaint);

    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }

    public void setClickable(boolean isClickable){
        this.isClickable = isClickable;
        if(isClickable){
            bitmapStrat = BitmapFactory.decodeResource(getResources(),R.drawable.ic_sport_start);
        }else {
            bitmapStrat = BitmapFactory.decodeResource(getResources(),R.drawable.ic_sport_start_disabled);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(isClickable){
            float x = event.getX();
            float y = event.getY();
            if ((x - leftOffset) * (x - leftOffset) + (y - radius) * (y - radius) < radius * radius) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    mOnButtonClickListener.onClick();
                    bitmapStrat = BitmapFactory.decodeResource(getResources(), R.drawable.ic_sport_start_pressed);
                }else if (event.getAction() == MotionEvent.ACTION_UP){
                    bitmapStrat = BitmapFactory.decodeResource(getResources(), R.drawable.ic_sport_start);
                }
                invalidate();
                return true;
            } else {
                return false;
            }
        }else {
            return false;
        }
    }

    public void setOnButtonClickListener(onButtonClickListener listener){
        this.mOnButtonClickListener = listener;
    }

    public interface onButtonClickListener{
        void onClick();
    }

}
